package net.jsoft.pms.web.mysql.desktop;

import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.jsoft.platform.core.util.platform.PlatformUtils;
import net.jsoft.platform.core.web.BaseController;
import net.jsoft.pms.entity.dept.Dept;
import net.jsoft.pms.entity.goal.GoalEntity;
import net.jsoft.pms.entity.grouprelationship.GroupRelationship;
import net.jsoft.pms.entity.plan.Plan;
import net.jsoft.pms.entity.ywfwqd.YwfwqdEntity;
import net.jsoft.pms.repository.goal.GoalRepository;
import net.jsoft.pms.repository.grouprelationship.GroupRelationshipRepository;
import net.jsoft.pms.repository.task.TaskRepository;
import net.jsoft.pms.repository.ywfwqd.YwfwqdRepository;

@Controller
@RequestMapping("/deskTop")
public class DeskTopController extends BaseController
{
    private String PREFIX = "/pms/mysql/desktop/";

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private GroupRelationshipRepository grRepository;

    @Autowired
    private GoalRepository goalRepository;

    @Autowired
    private YwfwqdRepository ywfwqdRepository;

    /**
     * 跳转到项目管理首页
     */
    @RequestMapping("/pms")
    public String index()
    {
        return PREFIX + "pms";
    }

    @RequestMapping("/mydesktop")
    public String mydesktop(Model model)
    {
        getProTask(model);
        getProGroupRelationship(model);
        getGoal(model);
        getYwfwqdUncheck(model);
        return "/pms/mysql/desktop/mydesktop";
    }

    @RequestMapping("/getYwfwqdUncheck")
    @ResponseBody
    private List<YwfwqdEntity> getYwfwqdUncheck(Model model)
    {

        Dept d = this.getUser().getDept();
        final long dId = d.getId();

        List<YwfwqdEntity> list = ywfwqdRepository.findAll(new Specification<YwfwqdEntity>()
        {

            @Override
            public Predicate toPredicate(Root<YwfwqdEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb)
            {
                Predicate p = null;
                try
                {

                    String dept = null;

                    switch (dId + "")
                    {
                    case "1":
                    {
                        dept = "申报缴税";
                        break;
                    }
                    case "2":
                    {
                        dept = "在线办税";
                        break;
                    }
                    case "3":
                    {
                        dept = "信息服务,门户权限";
                        break;
                    }
                    }

                    p = cb.greaterThan(root.<Date> get("lrrq"), PlatformUtils.getdate("2017-11-26"));
                    p = cb.and(cb.isNull(root.<Date> get("taskId")), p);

                    if (StringUtils.isNotEmpty(dept))
                    {
                        In<String> in = cb.in(root.<String> get("sszb"));
                        for (String s : dept.split(","))
                        {
                            in.value(s);
                        }

                        p = cb.and(in, p);
                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                return p;
            }
        });
        model.addAttribute("ywfwqd", list);
//        System.out.println(list.size());
        return list;
    }

    @RequestMapping("/getGoal")
    @ResponseBody
    private List<GoalEntity> getGoal(Model model)
    {
        List<GoalEntity> list = goalRepository.findUnfinished();
        model.addAttribute("goals", list);
        return list;
    }

    @RequestMapping("/getProTask")
    @ResponseBody
    public List<Plan> getProTask(Model model)
    {
        List<Plan> list = taskRepository.findProTask(this.getUser());
        model.addAttribute("tasks", list);
        return list;
    }

    @RequestMapping("/getProGroupRelationship")
    @ResponseBody
    public List<GroupRelationship> getProGroupRelationship(Model model)
    {
        List<GroupRelationship> list = grRepository.findProGroupRelationship(this.getUser());
        model.addAttribute("groupRelationships", list);
        return list;
    }
}
